﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmallERP.DAL;
using SmallERP.MODEL.FoundationModel;
using DevExpress.XtraGrid;
using System.Data;

namespace SmallERP.Bll.BaseInfo
{
    public class SysSet:IDBAccess
    {

        public SysSetting_info GetSysSet()
        {
            SysSetting_info sysSetInfo = new SysSetting_info();
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    SysSetting sysSet = entities.SysSetting.FirstOrDefault();
            //    if (sysSet != null)
            //    {
            //        sysSetInfo.BigPicSavePath = sysSet.BigPicSavePath;
            //        sysSetInfo.SysOrderPath = sysSet.SysOrderPath;
            //    }
            //    else
            //    {
            //        sysSetInfo = null;
            //    }
            //}

            string QuerySet = "select * from SysSetting";
            DataTable dt = DAL.AccessHelper.dataTable(QuerySet);
            if (dt.Rows.Count > 0)
            {
                sysSetInfo.BigPicSavePath = dt.Rows[0]["BigPicSavePath"].ToString();
                sysSetInfo.SysOrderPath = dt.Rows[0]["SysOrderPath"].ToString();
            }
            else
            {
                sysSetInfo = null;
            }
            return sysSetInfo;
        }
        #region IDBAccess 成员

        public int Add(object o)
        {
            throw new NotImplementedException();
        }

        public int Update(object o)
        {
            int ret = 0;
            SysSetting_info sysSetInfo =(SysSetting_info)o;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        SysSetting sysSet = entities.SysSetting.FirstOrDefault();

            //        sysSet.SysOrderPath = sysSetInfo.SysOrderPath;
            //        sysSet.BigPicSavePath = sysSetInfo.BigPicSavePath;
            //        entities.SaveChanges();
            //    }
            //    catch
            //    {
            //        ret = -1;
            //    }
            //}
            string UpdateSql = "update SysSetting set SysOrderPath='" + sysSetInfo.SysOrderPath + "',BigPicSavePath='" + sysSetInfo.BigPicSavePath + "'";
            try
            {
                ret = DAL.AccessHelper.ExecuteNonQuery(UpdateSql);
            }
            catch
            {
                ret = -2;
            }
            return ret;
        }

        public int Delete(object o)
        {
            throw new NotImplementedException();
        }

        public int LinkGridControl(object o)
        {
            int ret = 0;
            try
            {
                ((GridControl)o).DataSource = GetSysSet();
                ((GridControl)o).RefreshDataSource();
            }
            catch
            {
                ret = -1;
            }
            return ret;
        }

        #endregion
    }
}
